$(function () {
    /**
     * 查询模板信息
     */
    /*页面一加载，执行*/
    display(1, 5);

    function display(pageNoStr, pageSizeStr) {
        $.ajax({
            url: "../../template/selectTemplate",
            type: "post",
            data: {
                pageNoStr: pageNoStr,
                pageSizeStr: pageSizeStr
            },
            dataType: "json",
            success: function (map) {
                var templates = map.templates;
                var htmlStr = "";
                for (var i = 0; i < templates.length; i++) {
                    //创建时间
                    var createTime = templates[i].creationDates;
                    //模板创建名称
                    var createPage = templates[i].pageId.split(",");
                    var createName = "";
                    for (var z = 0; z < createPage.length; z++) {
                        createName = createPage[z];
                    }
                    //模板信息
                    var title = templates[i].title.split(",");
                    var len = title.length;
                    // htmlStr += "<td rowspan='"+len+"' style='line-height: "+(len*40)+"+px;' >"+createName+"</td>";
                    for (var j = 0; j < len; j++) {
                        var data = title[j].split("/");
                        var updateTime = "";    //更新日期
                        var update = "";    //更新者
                        var version_number = "";    //版本号
                        var state = ""; //状态
                        var pageId = "";
                        var num = 0;
                        for (var k = 0; k < data.length; k++) {
                            updateTime = data[1];
                            update = data[3];
                            version_number = data[0];
                            state = data[2];
                            pageId = templates[i].pageId;
                            if (k == 0) {
                                num = 1;
                            }
                        }
                        htmlStr += "<tr>";
                        if (j == 0) {
                            htmlStr += "<td rowspan='" + len + "' style='vertical-align:middle;' >" + createName + "</td>";
                        }
                        htmlStr += "<td >" + updateTime + "</td>";
                        htmlStr += "<td >" + update + "</td>";
                        htmlStr += "<td >" + version_number + "</td>";
                        htmlStr += "<td >" + createTime + "</td>";
                        var stateName = "";
                        if (state == 1) {
                            stateName = "未发布";
                            htmlStr += "<td >" + stateName + "</td>";
                            htmlStr += "<td >";
                            htmlStr += "<a href='one_template2.jsp?pageId=" + pageId + "&versionNumber=" + version_number + "'>";
                            htmlStr += "<i class='glyphicon glyphicon-search'></i>";
                            htmlStr += "</a>";
                            htmlStr += "<a href='../../template/deteleTemplate?pageId=" + pageId + "&versionNumber=" + version_number + "'>";
                            htmlStr += "<i class='glyphicon glyphicon-remove'></i>";
                            htmlStr += "</a>";
                            htmlStr += "<a href='one_template.jsp?pageId="+pageId+"&versionNumber="+version_number+"'>";
                            htmlStr += "<i class='glyphicon glyphicon-pencil'></i>";
                            htmlStr += "</a>";
                            htmlStr += "<a href='../../template/updatTemplateState?pageId="+pageId+"&versionNumber="+version_number+"&updatedDate="+updateTime+"'>";
                            htmlStr += "<i class='glyphicon glyphicon-ok'></i>"
                            htmlStr += "</a>";
                            /*  htmlStr += "<i class='glyphicon glyphicon-plus'></i>";*/
                            htmlStr += "</td>";
                        } else if (state == 2) {
                            stateName = "当前版本";
                            htmlStr += "<td >" + stateName + "</td>";
                            htmlStr += "<td >";
                            htmlStr += "<a href='one_template2.jsp?pageId=" + pageId + "&versionNumber=" + version_number + "'>";
                            htmlStr += "<i class='glyphicon glyphicon-search'></i>";
                            htmlStr += "</a>";
                            htmlStr += "<a href='../../template/deteleTemplate?pageId=" + pageId + "&versionNumber=" + version_number + "'>";
                            htmlStr += "<i class='glyphicon glyphicon-remove'></i>";
                            htmlStr += "</a>";
                            htmlStr += "<a href='one_template.jsp?pageId="+pageId+"&versionNumber="+version_number+"'>";
                            htmlStr += "<i class='glyphicon glyphicon-pencil'></i>";
                            htmlStr += "</a>";
                            /*  htmlStr += "<i class='glyphicon glyphicon-plus'></i>";*/
                            htmlStr += "</td>";
                        } else if (state == 3) {
                            stateName = "历史版本";
                            htmlStr += "<td >" + stateName + "</td>";
                            htmlStr += "<td >";
                            htmlStr += "<a href='one_template2.jsp?pageId=" + pageId + "&versionNumber=" + version_number + "'>";
                            htmlStr += "<i class='glyphicon glyphicon-search'></i>";
                            htmlStr += "</a>";
                            htmlStr += "<a href='../../template/deteleTemplate?pageId=" + pageId + "&versionNumber=" + version_number + "'>";
                            htmlStr += "<i class='glyphicon glyphicon-remove'></i>";
                            htmlStr += "</a>";
                            /*  htmlStr += "<i class='glyphicon glyphicon-plus'></i>";*/
                            htmlStr += "</td>";
                        }
                        htmlStr += "</tr>";
                    }
                }
                $("#tbodyHtml").html(htmlStr);
                /*页面插件-分页展示*/
                //计算总页数
                var totalPages = 1;
                if (map.count % pageSizeStr == 0) {
                    totalPages = map.count / pageSizeStr;
                } else {
                    totalPages = parseInt(map.count / pageSizeStr) + 1;
                }
                $("#query-page").bs_pagination({
                    currentPage: pageNoStr,	//当前页码
                    rowsPerPage: pageSizeStr,	//每页显示条数
                    totalRows: map.count,	//总条数
                    totalPages: totalPages,//总页数
                    visiblePageLinks: 5,	//最多显示的卡片数
                    showGoToPage: true,		//是否显示跳转到几页
                    showRowsPerPage: true,	//是否显示每页显示条数
                    showRowsInfo: true,		//是否显示记录条数
                    //用来监听页号切换的事件，event代表事件，pageobj代表页面信息
                    onChangePage: function (event, pageObj) {
                        display(pageObj.currentPage, pageObj.rowsPerPage);
                    }
                });
            }
        })
    }


})